package com.atguigu.tingshu.account.mq;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@Slf4j
public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService ;

    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
    public void initAccount(ConsumerRecord<String , String> record) {

        String value = record.value();      // 获取消息的内容
        if(!StringUtils.isEmpty(value)) {
            userAccountService.initAccount(Long.parseLong(value));
            log.info("用户账号初始化成功了....userId: {}" , value);
        }

    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void miunsAccount(ConsumerRecord<String , String> consumerRecord) {
        String orderNo = consumerRecord.value();
        if(!StringUtils.isEmpty(orderNo)) {
            userAccountService.miunsAccount(orderNo) ;
            log.info("用户账户金额扣减成功...orderNo: {}" , orderNo);
        }
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_UNLOCK)
    public void unLockAccount(ConsumerRecord<String , String> consumerRecord) {
        String orderNo = consumerRecord.value();
        if(!StringUtils.isEmpty(orderNo)) {
            userAccountService.unLockAccount(orderNo) ;
            log.info("用户账户金额解锁成功...orderNo: {}" , orderNo);
        }
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS)
    // 充值支付成功以后，增加账户金额
    public void rechargeInfoPaySuccess(ConsumerRecord<String , String> consumerRecord) {
        String orderNo = consumerRecord.value();
        if(!StringUtils.isEmpty(orderNo)) {
            userAccountService.rechargeInfoPaySuccess(orderNo) ;
            log.info("充值支付成功以后，增加账户金额成功了...orderNo: {}" , orderNo);
        }
    }
}
